﻿<script src="~/admui-13945148017-1592211513218/public/themes/global/js/components/bootstrap-datepicker.min.js"></script>
<script src="~/admui-13945148017-1592211513218/public/vendor/bootstrap-datepicker/bootstrap-datepicker.min.js"></script>
<script src="~/admui-13945148017-1592211513218/public/vendor/bootstrap-datepicker/bootstrap-datepicker.zh-CN.min.js"></script>
<link href="~/admui-13945148017-1592211513218/public/vendor/bootstrap-datepicker/bootstrap-datepicker.min.css" rel="stylesheet" />
<link href="~/admui-13945148017-1592211513218/public/css/examples/tables/datatable.css" rel="stylesheet" />
<link href="~/admui-13945148017-1592211513218/public/vendor/datatables-responsive/dataTables.responsive.css" rel="stylesheet" />
<link href="~/admui-13945148017-1592211513218/public/vendor/datatables-bootstrap/dataTables.bootstrap4.css" rel="stylesheet" />
<link href="~/admui-13945148017-1592211513218/public/vendor/datatables-responsive/dataTables.responsive.css" rel="stylesheet" />
<script src="~/admui-13945148017-1592211513218/public/vendor/datatables/jquery.dataTables.min.js"></script>
<script src="~/admui-13945148017-1592211513218/public/vendor/datatables-bootstrap/dataTables.bootstrap.min.js"></script>
<script src="~/admui-13945148017-1592211513218/public/vendor/datatables-bootstrap/dataTables.bootstrap4.js"></script>
<script src="~/admui-13945148017-1592211513218/public/vendor/datatables-responsive/dataTables.responsive.min.js"></script>
<script src="~/GoTRI/js/xlsx.full.min.js"></script>
<div class="page-container" id="admui-pageContent">
    <div class="page animation-fade page-forms">
        <div class="page-header">
            <h1 class="page-title">客户资源列表</h1>
            <ol class="breadcrumb">
                <li class="breadcrumb-item">
                    <a class="icon wb-home" href="javascript:;">客户资源管理</a>
                </li>
                <li class="breadcrumb-item active">客户资源列表</li>
            </ol>
        </div>
        <div class="page-content">
            <div class="panel">
                <div class="panel-body container-fluid">
                    <h4 class="example-title">选择员工</h4>
                    <div class="row">
                        <div class="form-group col-sm-3">
                            <label class="control-label" for="inputBasicPassword">员工名称</label>
                            @*<input type="text" class="form-control" id="ipt_YuanGongMingCheng" name="" placeholder="请输入员工名称" autocomplete="off" />*@
                            <select class="form-control" id="ipt_YuanGongMingCheng" name="" data-fv-notempty="true" data-fv-field="requiredSelect"></select>
                        </div>
                        <div class="form-group col-sm-3">
                            <br />
                            <button type="button" id="bt_ChaXun" class="btn btn-block btn-success" style="width: 100%; float: left;" onclick="FenPei()"><i class="icon wb-search"></i>提交</button>
                        </div>
                        <div class="form-group col-sm-3">
                            <br />
                            <input type="file" id="file" onchange="importExcel(this)" class="filter_input form-control" />
                            @*<button type="button" id="bt_ChongZhi" class="btn btn-outline btn-success" style="width: 100%;" onclick="ChongZhi();">重置</button>*@
                        </div>
                        <div class="form-group col-sm-3">
                            <br />
                            @*<input type="file" id="excel" />*@
                            <input type="submit" value="导入" id="drasd" class="btn btn-success" onclick="DaoRu()" />
                        </div>
                        @*</div>*@
                    </div>
                </div>
                <div class="panel-body">
                    <table class="table table-bordered table-hover dataTable table-striped w-full text-nowrap" id="dataTableExample" data-plugin="dataTable">
                        <thead>
                            <tr>
                                <th style="width: 2%; text-align: center;">
                                    <div class="checkbox-custom checkbox-inline checkbox-primary float-left">
                                        <input type="checkbox" name="checkAll" id="checkAll" value="全选" onclick='checkboxall();' /><label for="checkAll">全选</label>
                                    </div>
                                </th>
                                <th style="width: 50%;">手机号</th>
                                <th style="width: 48%;">状态</th>
                            </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function () {
        BangDingRenYuan();
        $("#SpanXiTong").click();
        bang(0);
    });

    function BangDingRenYuan() {
        $.ajax({
            type: 'POST',
            url: '../KeHuZiYuanChi/BangDingRenYuan',
            async: false,
            dataType: 'json',
            success: function (date) {
                var ddl = $("#ipt_YuanGongMingCheng");
                ddl.append("<option value='0'>请选择</option>");
                $(date).each(function (key) {
                    var opt = $("<option></option>").text(date[key].XingMing).val(date[key].Id);
                    ddl.append(opt);
                })
            },
        })
    }



    var selected = [];
    var currentData = [];
    var all = [];
    function bang(YuanGongMingCheng) {
        var lang = {
            "sProcessing": "大量处理中，请稍后...",
            "sLengthMenu": "每页 _MENU_ 项",
            "sZeroRecords": "没有匹配结果",
            "sInfo": "当前显示第 _START_ 至 _END_ 项，共 _TOTAL_ 项。",
            "sInfoEmpty": "当前显示第 0 至 0 项，共 0 项",
            "sInfoFiltered": "(由 _MAX_ 项结果过滤)",
            "sInfoPostFix": "",
            "sSearch": "搜索:",
            "sUrl": "",
            "sEmptyTable": "表中数据为空",
            "sLoadingRecords": "载入中...",
            "sInfoThousands": ",",
            "oPaginate": {
                "sFirst": "首页",
                "sPrevious": "上页",
                "sNext": "下页",
                "sLast": "末页",
                "sJump": "跳转"
            },
            "oAria": {
                "sSortAscending": ": 以升序排列此列",
                "sSortDescending": ": 以降序排列此列"
            }
        };
        $("#dataTableExample").dataTable().fnDestroy();
        $('#dataTableExample').dataTable({
            "order": [[0, "asc"]],
            "language": lang,
            "processing": true,
            "serverSide": true,
            //"bStateSave": true,//状态保持模式
            "searching": false,//禁用搜索
            ajax: function (data, callback, settings) {
                //封装请求参数
                var param = {};
                param.sw = "";
                param.limit = data.length;//页面显示记录条数，在页面显示每页显示多少项的时候
                param.start = data.start;//开始的记录序号
                param.page = (data.start / data.length) + 1;//当前页码
                param.dir = data.order[0].dir;
                param.column = data.order[0].column;

                param.YuanGongMingCheng = YuanGongMingCheng;
                //ajax请求数据
                $.ajax({
                    type: "POST",
                    url: "../KeHuZiYuanChi/KeHuZiYuanChiFenYe",
                    cache: false,  //禁用缓存
                    data: param,  //传入组装的参数
                    dataType: "json",
                    success: function (result) {
                        //setTimeout仅为测试延迟效果
                        setTimeout(function () {
                            //封装返回数据
                            var returnData = {};
                            returnData.draw = data.draw;//这里直接自行返回了draw计数器,应该由后台返回
                            returnData.recordsTotal = result.total;//返回数据全部记录
                            returnData.recordsFiltered = result.total;//后台不实现过滤功能，每次查询均视作全部结果
                            returnData.data = result.data;//返回的数据列表
                            //调用DataTables提供的callback方法，代表数据已封装完成并传回DataTables进行渲染
                            //此时的数据需确保正确无误，异常判断应在执行此回调前自行处理完毕"orderable" : false
                            callback(returnData);
                        }, 200);
                    }
                });
            },
            "rowCallback": function (row, data) {
                if ($.inArray(String(data.Id), self.selected) !== -1) {
                    $(row).find('input[type=checkbox]').prop('checked', true)
                }

            },
            //"columns": [
            "aoColumns": [{
                "sClass": "text-center",
                "data": "Id",
                "render": function (data, type, full, meta) {
                    return '<div class="checkbox-custom checkbox-inline checkbox-primary float-left"><input type="checkbox" name="xuanxiang" class="checkchild" value="' + data + '" /><label for="checkAll"></label></div>';
                },
                "bSortable": false
            },
            { "data": "ShouJiHao" },
            { "data": "ZhuangTai" }
            ]

        });
        
    }
    //全选按钮
    function checkboxall() {
        if ($('#checkAll').is(':checked')) {
            $("input[name='xuanxiang']").each(function () {
                $(this).prop("checked", true);
                selected.push(this.value);
            });
            //$(".checkchild").prop("checked", true);
        }
        else {
            $("input[name='xuanxiang']").each(function () {
                $(this).prop("checked", false);
                let index = self.selected.indexOf(this.value);
                self.selected.splice(index, 1);
            });
            //$(".checkchild").prop("checked", false);
        }
    }

    
        

    function ChaXun() {
        var YuanGongMingCheng = $("#ipt_YuanGongMingCheng").val();
        bang(YuanGongMingCheng);
    }
    function ChongZhi() {
        $("#ipt_YuanGongMingCheng").val(0);
        bang(0);
    }
    
    function FenPei() {
        var ipt_YuanGongMingCheng = $("#ipt_YuanGongMingCheng").find("option:selected").val();
        var obj = document.getElementsByName('xuanxiang'); //选择所有name="'test'"的对象，返回数组
        //取到对象数组后，我们来循环检测它是不是被选中
        var s = '';
        for (var i = 0; i < obj.length; i++) {
            if (obj[i].checked) {
                if (i != 0) {
                    s += ',' + obj[i].value;
                } else {
                    s += obj[i].value;
                }
            }
            
        }
        if (ipt_YuanGongMingCheng == "0") {
            alert("提交失败,未选择分配人员");
        }
        else if (s == '') {
            alert("提交失败,未选择配置手机号");
        } else {
            $.ajax({
                type: 'POST',
                url: '../KeHuZiYuanChi/FenPeiKeHu',
                data: { "KeHuZiYuanChiId": "" + s + "", "RenYuanId": "" + ipt_YuanGongMingCheng + "" },
                async: false,
                dataType: 'text',
                success: function (date) {
                    alert("分配成功");
                    bang(0);
                    //$(".checkAll").prop("checked", false)
                    //$(".checkAll").removeAttr("checked");
                    //$(this).prop("checked", false);
                },
            })
        }

    }

    var wb;//读取完成的数据
    var rABS = false; //是否将文件读取为二进制字符串
    function importExcel(obj) {//导入if(obj.files.length==0) {return;
        var f = obj.files[0];
        var reader = new FileReader();
        reader.onload = function (e) {
            data = e.target.result;
            if (rABS) {
                wb = XLSX.read(btoa(fixdata(data)), {//手动转化
                    type: 'base64'
                });
            } else {
                wb = XLSX.read(data, {
                    type: 'binary'
                });
            }
        };
        if (rABS) {
            reader.readAsArrayBuffer(f);
        } else {
            reader.readAsBinaryString(f);
        }
    }
    function fixdata(data) { //文件流转BinaryString
        var o = "",
            l = 0,
            w = 10240;
        for (; l < data.byteLength / w; ++l) o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w, l * w + w)));
        o += String.fromCharCode.apply(null, new Uint8Array(data.slice(l * w)));
        return o;
    }

    function DaoRu() {
        var aasd = wb.SheetNames[0];
        var b = wb.Sheets[aasd];
        var shuju = XLSX.utils.sheet_to_json(b);
        $.ajax({
            url: '../KeHuZiYuanChi/DaoRuExcel',
            data: { shuju: JSON.stringify(shuju) },
            type: "post",
            dataType: "text",
            success: function (response) {
                alert(response);
                bang(0);
            }
        });
    }
</script>